package com.lbjhan.rpc.consumer.handler;

import com.lbjhan.rpc.common.RpcRequest;
import com.lbjhan.rpc.serializer.ISerializer;
import com.lbjhan.rpc.serializer.KryoSerializer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class RpcNettyClientSerializerEncoder extends MessageToByteEncoder {

    private ISerializer serializer;
    public RpcNettyClientSerializerEncoder(ISerializer serializer){
        this.serializer = serializer;
    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf byteBuf) throws Exception {
        byte[] bytes = serializer.serialize(o);
        int length = bytes.length;
        byteBuf.writeInt(length);
        byteBuf.writeBytes(bytes);
    }

}
